﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Diagnostics;
using System.Configuration;
using System.Data;
using System.IO;
using Lead2012.Models;
using Lead2012.Helpers;
using Lead2012.ViewModels;
using Lead2012.Business;

namespace Lead2012.Controllers
{
	public abstract class BaseController : Controller
	{
        public void SetupCommon(BaseViewModel model, string keyword = "", string fieldid = "")
        {
            if (model != null)
            {
                model.Header = GetHeaderElement(keyword, fieldid);
                model.IsAdmin = model.Header.IsAdmin;
                model.UserId = model.Header.UserId.ToString();
                model.Menu = GetMenuNavigation(model.IsAdmin, model.PageType);
            }
        }

        #region #Common functions
        public HeaderModel GetHeaderElement(string keyword = "", string field = "")
        {
            HeaderModel header = new HeaderModel();
            header.LinkText = "Thoát";
            header.LinkUrl = "/Account/LogOff";
            header.EmailAdmin = System.Web.HttpContext.Current.User.Identity.Name;

            // Get user role
            header.IsAdmin = new EFRoleProvider().IsUserInRoleExtend(header.EmailAdmin, "Administrator");
            header.UserId = new UserRepository().GetSingle(header.EmailAdmin).ID;

            if (!string.IsNullOrEmpty(keyword))
                header.Keywords = keyword;
            if (!string.IsNullOrEmpty(field))
                header.FieldId = field;
            header.SearchListFilter = GetSearchListFilter();

            return header;
        }
        public List<ItemFilter> GetSearchListFilter()
        {
            List<ItemFilter> data = new List<ItemFilter>();

            Dictionary<string, string> list = new Dictionary<string, string>() { { "Name", "Tên khách hàng" }, { "Email", "Địa chỉ e-mail" }, { "Phone", "Số điện thoại" } };

            foreach (var ite in list)
            {
                ItemFilter item = new ItemFilter();
                item.ID = ite.Key;
                item.ItemName = ite.Value;
                data.Add(item);
            }

            return data;
        }

        public MenuModel GetMenuNavigation(bool isAdmin, string page)
        {
            MenuModel model = new MenuModel();
            if (isAdmin)
            {
                MenuModel menuAdmin = new MenuModel();
                string fileNameAdmin = "Configs/MenuAdmin.xml";
                string xmlPathAdmin = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileNameAdmin);
                model = (MenuModel)Helper.DeSerializeObject(xmlPathAdmin, typeof(MenuModel));
            }
            else
            {
                MenuModel menuAdmin = new MenuModel();
                string fileNameAdmin = "Configs/MenuUser.xml";
                string xmlPathAdmin = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileNameAdmin);
                model = (MenuModel)Helper.DeSerializeObject(xmlPathAdmin, typeof(MenuModel));
            }

            model.MenuList.ForEach(x => x.CurrentMenu = false);
            if (!string.IsNullOrEmpty(page))
            {
                switch (page)
                {
                    case "Leads":
                        model.MenuList.Single(x => x.TabName.Contains("Leads")).CurrentMenu = true;
                        break;
                    case "Dashboard":
                        model.MenuList.Single(x => x.TabName.Contains("Dashboard")).CurrentMenu = true;
                        break;
                    case "Menu":
                        model.MenuList.Single(x => x.TabName.Contains("Menu")).CurrentMenu = true;
                        break;
                }
            }

            model.EnableMenuNavigation = true;

            return model;
        }
        #endregion
    }
}
